<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2015/6/23
 * Time: 16:23
 */

class AuthModel extends Model{

    //读取所有菜单权限
    public function getAllMenus(){
        /*$main_list = M('Menu')->where(array('parent_id'=>0))->select();
        foreach ($main_list as $v) {
            
        }*/
        $menu = array();
        $list = M('Menu')->select();
        foreach ($list as $v) {
            $url = $v['path'] ? U($v['path']) : '#';
            $menu[$v['id']] = array('title'=>$v['name'],'url'=>$url,'icon'=>$v['icon']);
        }
        return $menu;
    }

    //初始化角色拥有的菜单权限
    public function initAuth($auth){
        $authArr = json_decode($auth,true);
        $allmenus = $this->getAllMenus();
        $menu = array();
        foreach ($authArr as $k=>$v) {
            $menu['main'][$k] = $allmenus[$k];
            if(!empty($v)){
                foreach ($v as $vv) {
                    $menu['sub'][$k][$vv] = $allmenus[$vv];
                }
            }
        }
        return $menu;
    }

    public function getMenuIcon($auth){
        $authArr = json_decode($auth,true);
        $allmenus = $this->getAllMenus();
        $menu = $indexIcon = array();
        foreach ($authArr as $k=>$v) {
            //$menu['main'][$k] = $allmenus[$k];
            $indexIcon[$k]['icon'] = APP_DOMAIN.$allmenus[$k]['icon'];
            if($allmenus[$k]['url']!='#'){
                $indexIcon[$k]['url'] = $allmenus[$k]['url'];
            }else{
                $subMenuFistId = $v[0];
                $indexIcon[$k]['url'] = $allmenus[$subMenuFistId]['url'];
            }
        }
        return $indexIcon;
    }
}